<?php
/**
 * 
 * Purchase ledger
 * 
 * @category Abovo
 * 
 * @package Abovo_App_Buy
 * 
 * @subpackage Abovo_App_Buy_Ledger
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 * 
 * @version $Id$
 * 
 */

/**
 * 
 * Purchase ledger
 * 
 * @category Abovo
 * 
 * @package Abovo_App_Buy
 * 
 * @subpackage Abovo_App_Buy_Ledger
 * 
 */
class Abovo_App_Buy_Ledger extends Abovo_App_Buy {
    
    /**
     * 
     * undocumented class variable
     * 
     * @var string
     * 
     */
    public $form_new;
    
    /**
     * 
     * undocumented class variable
     * 
     * @var string
     * 
     */
    public $purchases;
    
    /**
     * 
     * undocumented class variable
     * 
     * @var string
     * 
     */
    protected $_action_default = 'browse';
    
    /**
     * 
     * undocumented class variable
     * 
     * @var string
     * 
     */
    protected $_pledger;
    
    /**
     * 
     * undocumented function
     * 
     * @return void
     * 
     */
    public function actionBrowse()
    {
        $this->purchases = $this->_pledger->fetchAllByStatus('new', 'duedate');
    }
    
    /**
     * 
     * undocumented function
     * 
     * @return void
     * 
     */
    public function actionNew()
    {
        $form = $this->_pledger->form();
        
        if ($this->_isProcess('save')) {
            $day   = $this->_request->post('day', false);
            $month = $this->_request->post('month', false);
            $year  = $this->_request->post('year', false);
            
            if (! ($day && $month && $year)) {
                $date = date('Y-m-d');
            } else {
                $date = date('Y-m-d',
                    strtotime((int) $day . '-' . (int) $month . '-' . (int) $year)
                );
            }
            
            if ($form->validate()) {
                $data = $form->values();
                $data = $data['order'];
                
                $data['duedate']        = $date;
                $data['handle_created'] = $this->user->handle;
                $data['handle_updated'] = $this->user->handle;
                $data['status']         = 'new';
                
                $in = $this->_pledger->save($data);
            }
        }
        
        $this->form_new = $form;
    }
    
    /**
     * 
     * undocumented function
     * 
     * @return void
     * 
     */
    protected function _setUp()
    {
        parent::_setUp();
        $this->_pledger = Solar::factory('Abovo_Model_Order_Pledger');
        
        $this->layout_local['buy/ledger/browse'] = $this->locale('APP_ACTION_BROWSE');
        $this->layout_local['buy/ledger/new'] = $this->locale('APP_ACTION_NEW');
    }
}